//
//  BYJCarouselViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/17.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "BYJCarouselViewController.h"

@interface BYJCarouselViewController ()<UITableViewDelegate, UITableViewDataSource, UMSocialUIDelegate>

/** tableview */
@property (nonatomic, retain) UITableView *CarouselTbaleView;
/** 头视图 */
@property (nonatomic, retain) UIImageView *imgView;
/** 第一个分区视图 */
@property (nonatomic, retain) UIView *headView;

@property (nonatomic, retain) BYJCarousModel *model;

@end

@implementation BYJCarouselViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.model = [[BYJCarousModel alloc] init];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self dataHandle];
}

#pragma mark - 创建tableview
- (void)createTableView {
    
    self.CarouselTbaleView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 64 - 59) style:UITableViewStylePlain];
    _CarouselTbaleView.delegate=self;
    _CarouselTbaleView.dataSource=self;
    _CarouselTbaleView.backgroundColor = [UIColor whiteColor];
    _CarouselTbaleView.showsVerticalScrollIndicator = NO;
    _CarouselTbaleView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_CarouselTbaleView];
    
    self.imgView =[[UIImageView alloc]init];
    _imgView.backgroundColor = [UIColor cyanColor];
    _imgView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH / 3);
    [_imgView sd_setImageWithURL:[NSURL URLWithString:_model.pic] placeholderImage:PLACEHOLDER];
    
    _CarouselTbaleView.tableHeaderView = _imgView;
    
    
    [_CarouselTbaleView registerClass:[BYJCarouselTableViewCell class] forCellReuseIdentifier:@"BYJCarouselTableViewCell"];
    
    [_CarouselTbaleView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

#pragma mark 分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark 分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

#pragma mark 自定义分区
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    _headView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
    
    BYJView *likesView = [[BYJView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 8 - 25, 0, 50, 50)];
    [self.headView addSubview:likesView];
    [likesView.BYJButton addTarget:self action:@selector(likesButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [likesView.BYJButton setImage:[[UIImage imageNamed:@"list_menu_unfavorite.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    likesView.BYJLabel.text = [NSString stringWithFormat:@"%@", _model.favorite_count];
    
    BYJView *commentView = [[BYJView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 / 8 - 25, 0, 50, 50)];
    //commentView.backgroundColor = [UIColor redColor];
    [self.headView addSubview:commentView];
    [commentView.BYJButton addTarget:self action:@selector(commentButton:) forControlEvents:UIControlEventTouchUpInside];
    [commentView.BYJButton setImage:[UIImage imageNamed:@"list_menu_comment"] forState:UIControlStateNormal];
    commentView.BYJLabel.text = [NSString stringWithFormat:@"%@", _model.comment_count];
    
    BYJView *shareView = [[BYJView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 5 / 8 - 25, 0, 50, 50)];
    //shareView.backgroundColor = [UIColor redColor];
    [self.headView addSubview:shareView];
    [shareView.BYJButton addTarget:self action:@selector(shareButton:) forControlEvents:UIControlEventTouchUpInside];
    [shareView.BYJButton setImage:[UIImage imageNamed:@"list_menu_share"] forState:UIControlStateNormal];
    shareView.BYJLabel.text = [NSString stringWithFormat:@"%@", _model.share_count];
    
    BYJView *downloadView = [[BYJView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 7 / 8 - 25, 0, 50, 50)];
    //downloadView.backgroundColor = [UIColor redColor];
    [self.headView addSubview:downloadView];
    [downloadView.BYJButton addTarget:self action:@selector(downloadButton:) forControlEvents:UIControlEventTouchUpInside];
    [downloadView.BYJButton setImage:[UIImage imageNamed:@"list_menu_download"] forState:UIControlStateNormal];
    downloadView.BYJLabel.text = @"下载";
    return _headView;
}

- (void)likesButtonAction:(UIButton *)button {
    
}

- (void)commentButton:(UIButton *)button {
    
}

- (void)shareButton:(UIButton *)button {
    
    [PlayViewController sharePlayViewBar].hidden = YES;
    
    NSString *url = [NSString stringWithFormat:@"http://quan.dongting.com/do.html?m=music&a=details&id=%@", _value];
    
    // 弹出分享页面
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56f9e113e0f55a5f1b00159e"
                                      shareText:url
                                     shareImage:[UIImage imageNamed:@""]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,
                                    UMShareToQzone,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToWechatSession, UMShareToWechatTimeline, UMShareToWechatFavorite,nil]
                                       delegate:nil];
}

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        [PlayViewController sharePlayViewBar].hidden = NO;
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    } else {
        [PlayViewController sharePlayViewBar].hidden = NO;
    }
}

- (void)downloadButton:(UIButton *)button {
    
}

#pragma mark cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.songsArr.count + 1;
}

#pragma mark cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 70;
    } else {
        return 60;
    }
}

#pragma 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        
        UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        playButton.frame = CGRectMake(10, 20, 30, 30);
        [playButton setImage:[UIImage imageNamed:@"PlayPressed"] forState:UIControlStateNormal];
        [playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:playButton];
        
        UILabel *playLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 100, 30)];
        playLabel.text= @"播放全部";
        [cell addSubview:playLabel];
        
        UIButton *manageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        manageButton.frame = CGRectMake(SCREEN_WIDTH - 50, 20, 50, 50);
        [manageButton setImage:[UIImage imageNamed:@"audioeffect_equalizer_checked"] forState:UIControlStateNormal];
        [manageButton addTarget:self action:@selector(manageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:manageButton];
        
        return cell;
    }
    
    BYJCarouselTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BYJCarouselTableViewCell" forIndexPath:indexPath];
    BYJsongListModel *model = _model.songsArr[indexPath.row - 1];
    cell.model = model;
    cell.indexPath = indexPath.row;
    cell.block = ^(BYJmvListModel *model) {
        
        BYJMVViewController *mvVc = [[BYJMVViewController alloc] init];
        mvVc.model = model;
        mvVc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:mvVc animated:YES];
        [self presentViewController:mvVc animated:YES completion:^{
            
        }];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DSYMusicViewController *musicVC = [DSYMusicViewController sharedMusicPlayingVC];
    
    if (indexPath.row == 0) {
        musicVC.index = 0;
    } else {
        
        musicVC.index = indexPath.row - 1;
    }
    musicVC.isLocal = NO;

    
    DSYNAVViewController *nav = [DSYNAVViewController shareNavWithRootViewController:musicVC];
    
    
    [PlayViewController sharePlayViewBar].hidden = YES;
    
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)playButtonAction:(UIButton *)button {
    
}

- (void)manageButtonAction:(UIButton *)button {
    
    BYJManageViewController *VC = [[BYJManageViewController alloc] init];
    VC.dataSource = _model.songsArr;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 数据解析
- (void)dataHandle {
    
    NSString *url = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?", _value];
    
    [APPTools GETWithURL:url par:nil success:^(id responseObject) {
     
        [_model setValuesForKeysWithDictionary:responseObject];
        NSMutableArray *songList = [DSYSongModel modelHandleWithArray:responseObject[@"songs"]];
        [DSYDataManager shareDataManager].allDataArray = songList;
        
        [self createTableView];
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
